package com.taptech.doufu.ui.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taptech.doufu.R;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.ui.adapter.GiftUserListAdapter;
import com.taptech.doufu.bean.cartoon.CartoonBean;
import com.taptech.doufu.bean.MineNovelBean;
import com.taptech.doufu.bean.PersonalNoteBean;
import com.taptech.doufu.bean.ProductionBean;
import com.taptech.doufu.bean.UserGiftRecordBean;
import com.taptech.doufu.constant.Constant;
import com.taptech.doufu.ui.view.drawcircle.MyRecyclerView;
import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.services.GiftService;
import com.taptech.doufu.util.DiaobaoUtil;
import com.taptech.doufu.util.GlideUtil;
import com.taptech.doufu.util.TMAnalysis;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.net.httputils.HttpUtil;
import com.taptech.doufu.ui.view.GiftDialogManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by longsiyang on 16/1/8.
 */
public class GiftUserListActivity extends DiaobaoBaseActivity implements View.OnClickListener, HttpResponseListener , GiftDialogManager.OnCloseGiftDialogListener {

    ProductionBean bean;

    // 分类数据
    MineNovelBean novelBean;
    CartoonBean cartoonBean;
    PersonalNoteBean noteBean;
    private int object_type =  0;

    List<UserGiftRecordBean> mDataList;
    // UI View
    TextView contentTitle, authorName, giftCount;
    MyRecyclerView userListView;
    GiftUserListAdapter adapter;
    // bottom UI View
    ImageView userHeadIcon;
    TextView sendGiftCount , sendGiftRank;
    TextView giveGiftBtn;
    View jumpToLoginBtn;
    RelativeLayout giftSendLayout;
    // 送礼物
    GiftDialogManager giftDialogManager;

    private TextView mRuchGift;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addShadeBg();
        setContentView(R.layout.gift_userlist_activity_layout);
        initData();
        initView();
        GiftService.getInstance().loadGiftRankList(this , bean.getId() , bean.getObject_type());
    }

    private void initData() {
        Intent intent = getIntent();
        bean = (ProductionBean) intent.getSerializableExtra(Constant.DATA);
        if (bean instanceof CartoonBean) {
            cartoonBean = (CartoonBean) intent.getSerializableExtra(Constant.DATA);
            object_type = Constant.OBJECT_TYPE_CARTOON;
        } else if (bean instanceof MineNovelBean) {
            novelBean = (MineNovelBean) intent.getSerializableExtra(Constant.DATA);
            bean.setObject_type("18");
            object_type = 18;
        } else if (bean instanceof PersonalNoteBean){
            noteBean = (PersonalNoteBean) intent.getSerializableExtra(Constant.DATA);
            object_type = Integer.parseInt(noteBean.getObject_type());
        }
        mDataList = new ArrayList<>();
    }

    private void initView() {
        userHeadIcon = (ImageView) findViewById(R.id.user_icon);
        contentTitle = (TextView) findViewById(R.id.content_title);
        authorName = (TextView) findViewById(R.id.author_name);
        giftCount = (TextView) findViewById(R.id.gift_total_count);
        sendGiftCount = (TextView) findViewById(R.id.send_gift_count_textview);
        sendGiftRank = (TextView) findViewById(R.id.gift_rank_textview);
        giveGiftBtn = (TextView) findViewById(R.id.give_gift_btn);
        giveGiftBtn.setOnClickListener(this);
        jumpToLoginBtn = (View) findViewById(R.id.jump_to_login_btn);
        jumpToLoginBtn.setOnClickListener(this);
        giftSendLayout = (RelativeLayout) findViewById(R.id.gift_send_layout);
        giftSendLayout.setOnClickListener(this);
        userListView = (MyRecyclerView) findViewById(R.id.user_gift_listview);
        mRuchGift = (TextView) findViewById(R.id.ruch_web_intent);
        mRuchGift.setOnClickListener(this);
        RecyclerView.LayoutManager layoutManager = new StaggeredGridLayoutManager(1 , StaggeredGridLayoutManager.VERTICAL);
        userListView.setLayoutManager(layoutManager);
        adapter = new GiftUserListAdapter(this , mDataList);
        userListView.setAdapter(adapter);
        switch (object_type){
            case Constant.OBJECT_TYPE_CARTOON:
                contentTitle.setText(cartoonBean.getTitle());
                break;
            case Constant.OBJECT_TYPE_NOTE:
                contentTitle.setText(noteBean.getTitle());
                break;
            case Constant.OBJECT_TYPE_NOVEL:
                contentTitle.setText(novelBean.getTitle());
                break;
        }

    }


    @Override
    public void handleResponse(int handleType, HttpResponseObject response) {
        HttpUtil.setOnHandleHttpResponListener(this, handleType, response, new HttpUtil.HandleHttpResponListener() {
            @Override
            public void handleRespon(int handleType, JSONObject data) {
                switch (handleType){
                    case GiftService.HANDLE_LOAD_GIFT_RANK_LIST:
                        try {
                            List<UserGiftRecordBean> dataList = DiaobaoUtil.json2list(UserGiftRecordBean.class , data.getJSONArray("reward_records"));
                            if (mDataList != null && adapter != null){
                                mDataList.clear();
                                mDataList.addAll(dataList);
                                adapter.notifyDataSetChanged();
                            }
                            if (bean != null){
                                bean.setJson2(data.getJSONObject("object"));
                                if (bean.getObject_type().equals("5")){
                                    bean.setObject_type("18");
                                    object_type = 18;
                                }
                                handleTopInfo(bean);
                            }
                            if (data.has("mine")){
                                JSONObject mineUserData = data.getJSONObject("mine");
                                sendGiftRank.setVisibility(View.VISIBLE);
                                handleBottomInfo(mineUserData);
                            }else {
                                sendGiftCount.setText("您还未登录");
                                sendGiftRank.setVisibility(View.GONE);
                                //jumpToLoginBtn.setVisibility(View.VISIBLE);
                            }

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        GiftService.getInstance().loadGiftRankList(this, bean.getId(), bean.getObject_type());
    }

    private void handleTopInfo(ProductionBean bean){
        if (bean == null){
            return;
        }
        if (bean.getTitle() != null){
            contentTitle.setText(bean.getTitle());
        }
        if (bean.getUser() != null && bean.getUser().getName() != null){
            authorName.setText(bean.getUser().getName());
        }
        if (bean.getReward_sum() != null){
            giftCount.setText("共计" + bean.getReward_sum() + "豆币");
        }
    }

    private void handleBottomInfo(JSONObject mineUserData){
        if (mineUserData != null){
            /*RoundImageView userHeadIcon;
            TextView sendGiftCount , sendGiftRank;
            TextView giveGiftBtn;*/
            UserGiftRecordBean mineBean = new UserGiftRecordBean();
            mineBean.setJson2(mineUserData);
            GlideUtil.displayCircleImageBorder(userHeadIcon, mineBean.getFrom_user().getIcon());
            if (mineBean.getRank() != null){
                if (mineBean.getRank().equals("0")){
                    sendGiftCount.setText("暂时还未打赏作者");
                    if (bean!= null) {
                        if (bean.getObject_type().equals("18")) {
                            sendGiftRank.setText("催更礼物规则戳右上角");
                            sendGiftRank.setTextColor(Color.parseColor("#b1b1b1"));
                        }else {
                            sendGiftRank.setVisibility(View.GONE);
                        }
                    }
                    giveGiftBtn.setText("打赏作者");
                }else {
                    sendGiftCount.setText("送出" + mineBean.getCount() + "个礼物值" + mineBean.getCoins() + "豆币");
                    sendGiftRank.setVisibility(View.VISIBLE);
                    sendGiftRank.setText("排名" + mineBean.getRank() + "位");
                    giveGiftBtn.setText("继续打赏");
                }
            }


        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.give_gift_btn:
                Map keyMap = new HashMap();
                keyMap.put("mid" , bean.getId());
                keyMap.put("mtype" , bean.getObject_type() );
                keyMap.put("muid" , bean.getUser().getUserId());
                TMAnalysis.event(this, "continue_to_give_gift");
                giftDialogManager = GiftDialogManager.getInstance(this , bean);
                giftDialogManager.setOnCloseGiftDialogListener(this);
                break;
            case R.id.jump_to_login_btn:
                AccountService.getInstance().jumpToLogin();
                break;
            case R.id.gift_send_layout:
                if (!AccountService.getInstance().isLogin()){
                    AccountService.getInstance().jumpToLogin();
                }
                break;
            case R.id.ruch_web_intent://跳转到催更web
                GiftService.enterRuchWebActivity(this);
                break;
            default:
                break;
        }
    }

    @Override
    public void onCloseGiftDialog() {
        GiftService.getInstance().loadGiftRankList(this, bean.getId(), bean.getObject_type());
    }
}
